package com.yds.developtools.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.yds.developtools.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：于德水
 * 创建时间：2021/9/9 0009 16:50
 * 创建描述：空视图布局
 */
@SuppressWarnings("all")
public class YDSEmptyContentLayout extends LinearLayout {
    private View emptyView;
    private ImageView ivEmptyContent;
    private TextView tvEmptyContent;

    private float tipsMarginLeft = 0;
    private float tipsMarginRight = 0;
    private float tipsMarginTop = 10;
    private float tipsMarginBottom = 0;

    private float tipsPaddingLeft = 0;
    private float tipsPaddingRight = 0;
    private float tipsPaddingTop = 0;
    private float tipsPaddingBottom = 0;

    private float viewsMarginLeft = 75;
    private float viewsMarginRight = 75;
    private float viewsMarginTop = 0;
    private float viewsMarginBottom = 0;

    private float viewsPaddingLeft = 0;
    private float viewsPaddingRight = 0;
    private float viewsPaddingTop = 0;
    private float viewsPaddingBottom = 0;

    //空布局点击时间回调
    private OnEmptyViewClickListener mOnEmptyViewClickListener;

    public YDSEmptyContentLayout(Context context) {
        this(context, null);
    }

    public YDSEmptyContentLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YDSEmptyContentLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setOrientation(VERTICAL);
        initEmptyView(context, attrs);
    }

    private void initEmptyView(Context context, @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        emptyView = LayoutInflater.from(getContext()).inflate(R.layout.layout_yds_empty_content, this, false);
        ivEmptyContent = emptyView.findViewById(R.id.ivEmptyContent);
        tvEmptyContent = emptyView.findViewById(R.id.tvEmptyContent);

        ivEmptyContent.setOnClickListener(v -> {
            if (null != mOnEmptyViewClickListener) {
                mOnEmptyViewClickListener.onEmptyClick();
            }
        });

        tvEmptyContent.setOnClickListener(v -> {
            if (null != mOnEmptyViewClickListener) {
                mOnEmptyViewClickListener.onEmptyClick();
            }
        });

        initEmptyTipsAttribute(context, attrs);
    }

    private void initEmptyTipsAttribute(Context context, @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.YDSEmptyContentLayout);

        //设置空布局视图是否可见
        boolean emptyIconVisible = typedArray.getBoolean(R.styleable.YDSEmptyContentLayout_emptyContentIconVisible, true);
        setEmptyContentIconVisible(emptyIconVisible);
        //设置空布局视图
        int emptyViewId = typedArray.getResourceId(R.styleable.YDSEmptyContentLayout_emptyContentIcon, R.drawable.ic_baseline_empty);
        setEmptyContentIcon(emptyViewId);
        //设置空布局提示视图 padding
        viewsPaddingLeft = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewPaddingLeft, 0);
        viewsPaddingTop = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewPaddingTop, 0);
        viewsPaddingRight = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewPaddingRight, 0);
        viewsPaddingBottom = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewPaddingBottom, 0);
        setEmptyContentViewPaddings(viewsPaddingLeft, viewsPaddingTop, viewsPaddingRight, viewsPaddingBottom);
        //设置空布局提示视图 margin
        viewsMarginLeft = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewMarginLeft, 75);
        viewsMarginTop = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewMarginTop, 0);
        viewsMarginRight = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewMarginRight, 75);
        viewsMarginBottom = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentViewMarginBottom, 0);
        setEmptyContentViewMargins(viewsMarginLeft, viewsMarginTop, viewsMarginRight, viewsMarginBottom);

        //设置Tips文字是否可见
        boolean emptyContentTipsVisible = typedArray.getBoolean(R.styleable.YDSEmptyContentLayout_emptyContentTipsVisible, true);
        setEmptyContentTipsVisible(emptyContentTipsVisible);
        //设置Tips文字
        String emptyContentTips = typedArray.getString(R.styleable.YDSEmptyContentLayout_emptyContentTips);
        if (!TextUtils.isEmpty(emptyContentTips)) {
            setEmptyContentTips(emptyContentTips);
        }
        //设置Tips文字样式
        String emptyContentTipsStyle = typedArray.getString(R.styleable.YDSEmptyContentLayout_emptyContentTipsStyle);
        setEmptyContentTipsStyle(emptyContentTipsStyle);
        //设置Tips文字颜色
        int emptyContentTipsColorId = typedArray.getResourceId(R.styleable.YDSEmptyContentLayout_emptyContentTipsColor, R.color.black);
        setEmptyContentTipsColor(emptyContentTipsColorId);
        //设置Tips文字大小
        float emptyContentTipsSize = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsSize, 14f);
        setEmptyContentTipsSize(emptyContentTipsSize);
        //设置tips文字 padding
        tipsPaddingLeft = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsPaddingLeft, 0);
        tipsPaddingTop = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsPaddingTop, 0);
        tipsPaddingRight = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsPaddingRight, 0);
        tipsPaddingBottom = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsPaddingBottom, 0);
        setEmptyContentTipsPaddings(tipsPaddingLeft, tipsPaddingTop, tipsPaddingRight, tipsPaddingBottom);
        //设置tips文字 margin
        tipsMarginLeft = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsMarginLeft, 0);
        tipsMarginTop = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsMarginTop, 10);
        tipsMarginRight = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsMarginRight, 0);
        tipsMarginBottom = typedArray.getFloat(R.styleable.YDSEmptyContentLayout_emptyContentTipsMarginBottom, 0);
        setEmptyContentTipsMargins((int) tipsMarginLeft, (int) tipsMarginTop, (int) tipsMarginRight, (int) tipsMarginBottom);
    }

    /**
     * 设置空布局 是否可见
     *
     * @param visible
     */
    LinkedHashMap<Integer, View> mMapViews = new LinkedHashMap<>();

    public void setEmptyContentViewVisible(boolean visible) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child != emptyView) {
                mMapViews.put(child.hashCode(), child);
            }
        }

        removeAllViews();
        if (visible) {
            addView(emptyView);
        } else {
            Iterator<Map.Entry<Integer, View>> iterator = mMapViews.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, View> entry = iterator.next();
                addView(entry.getValue());
            }
        }
    }

    /**
     * 设置空布局提示图标是否可见
     *
     * @param visible
     */
    public void setEmptyContentIconVisible(boolean visible) {
        ivEmptyContent.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置空布局提示图标
     *
     * @param resourceId
     */
    public void setEmptyContentIcon(int resourceId) {
        ivEmptyContent.setImageResource(resourceId);
    }

    /**
     * 设置空布局提示视图 padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setEmptyContentViewPaddings(float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) {
        ivEmptyContent.setPadding(dpToPx(paddingLeft), dpToPx(paddingTop), dpToPx(paddingRight), dpToPx(paddingBottom));
    }

    /**
     * 设置空布局提示视图 margin
     *
     * @param marginLeft
     * @param marginTop
     * @param marginRight
     * @param marginBottom
     */
    public void setEmptyContentViewMargins(float marginLeft, float marginTop, float marginRight, float marginBottom) {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) ivEmptyContent.getLayoutParams();
        layoutParams.setMargins(dpToPx(marginLeft), dpToPx(marginTop), dpToPx(marginRight), dpToPx(marginBottom));
        ivEmptyContent.setLayoutParams(layoutParams);
    }

    /**
     * 设置空布局提示文字是否可见
     *
     * @param visible
     */
    public void setEmptyContentTipsVisible(boolean visible) {
        tvEmptyContent.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置空布局提示文字
     *
     * @param tips
     */
    public void setEmptyContentTips(String tips) {
        tvEmptyContent.setText(tips);
    }

    /**
     * 设置空布局提示文字字体样式
     *
     * @param type
     */
    public void setEmptyContentTipsStyle(String type) {
        if (TextUtils.isEmpty(type)) return;
        if (type.equals("normal")) {
            tvEmptyContent.setTypeface(Typeface.DEFAULT);
        } else if (type.equals("bold")) {
            tvEmptyContent.setTypeface(Typeface.DEFAULT_BOLD);
        }
    }

    /**
     * 设置空布局提示文字颜色
     *
     * @param colorId
     */
    public void setEmptyContentTipsColor(int colorId) {
        tvEmptyContent.setTextColor(getContext().getResources().getColor(colorId));
    }

    /**
     * 设置空布局提示文字大小
     *
     * @param sp 默认单位 sp
     */
    public void setEmptyContentTipsSize(float sp) {
        tvEmptyContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    /**
     * 设置空布局提示文字 padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setEmptyContentTipsPaddings(float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) {
        tvEmptyContent.setPadding(dpToPx(paddingLeft), dpToPx(paddingTop), dpToPx(paddingRight), dpToPx(paddingBottom));
    }

    /**
     * 设置空布局提示文字 marign
     *
     * @param marginLeft
     * @param marginTop
     * @param marginRight
     * @param marginBottom
     */
    public void setEmptyContentTipsMargins(float marginLeft, float marginTop, float marginRight, float marginBottom) {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tvEmptyContent.getLayoutParams();
        layoutParams.setMargins(dpToPx(marginLeft), dpToPx(marginTop), dpToPx(marginRight), dpToPx(marginBottom));
        tvEmptyContent.setLayoutParams(layoutParams);
    }

    /**
     * 设置空视图点击事件回调
     *
     * @param clickCallBack
     */
    public void setOnEmptyViewClickListener(OnEmptyViewClickListener listener) {
        mOnEmptyViewClickListener = listener;
    }

    /**
     * 空视图点击事件回调
     */
    public interface OnEmptyViewClickListener {
        void onEmptyClick();
    }

    /**
     * px to sp
     *
     * @param pxValue
     * @return
     */
    private int pxToSp(float pxValue) {
        float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * dp to px
     */
    public int dpToPx(float dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5);
    }
}
